package com.itheima.day17.xmlparse.test1;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.lang.model.util.Elements;
import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

public class Test {
    public static void main(String[] args) throws DocumentException {
        // 1.获取一个解析器对象
        SAXReader saxReader = new SAXReader();

        // 2.利用解析器把xml文件加载到内存中，并返回一个的文档对象
        Document document = saxReader.read(new File("myCode\\xml\\student.xml"));

        // 3.获取跟标签
        Element rootElement = document.getRootElement();

        // 4.通过跟标签来获取student标签
        List<Element> studentElements = rootElement.elements("student");

        HashSet<Student> hs = new HashSet<>();

        // 5.遍历集合，得到每一个student标签
        for (Element element : studentElements) {
            // 获得id属性值
            Attribute attribute = element.attribute("id");
            String id = attribute.getValue();

            // 获得name属性值
            Element nameElement = element.element("name");
            String name = nameElement.getText();

            // 获得age属性
            Element ageElement = element.element("age");
            String age = ageElement.getText();

            /*System.out.println(id);
            System.out.println(name);
            System.out.println(age);*/

            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setAge(Integer.parseInt(age));

            hs.add(student);
        }

        Iterator<Student> it = hs.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            if("1".equals(stu.getId())){
                it.remove();
            }
            System.out.println(it.next());
        }

    }
}
